package com.advertisementsystem.Controller;
import com.advertisementsystem.Exception.ResponseResult;
import com.advertisementsystem.Servie.IncomeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/income")
@Slf4j
public class IncomeController {

    @Autowired
    private IncomeService incomeService;

    /**
     * 获取月度收入数据
     * @return 包含月度收入总额和环比增长率的数据
     */
    @GetMapping("/monthly")
    @PreAuthorize("hasAuthority('income:view')")
    public ResponseResult<Map<String, Object>> getMonthlyIncome() {
        log.info("获取月度收入数据");
        Map<String, Object> incomeData = incomeService.getMonthlyIncome();
        return ResponseResult.success(incomeData);
    }

    /**
     * 获取年度收入统计
     * @param year 年份，可选参数，默认为当前年
     * @return 年度收入统计数据
     */
    @GetMapping("/yearly-stats")
    @PreAuthorize("hasAuthority('income:view')")
    public ResponseResult<Map<String, Object>> getYearlyIncomeStats(
            @RequestParam(required = false) Integer year) {
        log.info("获取年度收入统计，年份：{}", year);
        Map<String, Object> statsData = incomeService.getYearlyIncomeStats(year);
        return ResponseResult.success(statsData);
    }

    /**
     * 获取收入明细
     * @param status 账单状态筛选，可选值：PAID, UNPAID, OVERDUE, ALL，默认ALL
     * @param pageNum 页码，默认1
     * @param pageSize 每页大小，默认10
     * @return 收入明细分页数据
     */
    @GetMapping("/details")
    @PreAuthorize("hasAuthority('income:view')")
    public ResponseResult<Page<Map<String, Object>>> getIncomeDetails(
            @RequestParam(required = false, defaultValue = "ALL") String status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取收入明细，状态：{}，页码：{}，每页大小：{}", status, pageNum, pageSize);
        Page<Map<String, Object>> page = incomeService.getIncomeDetails(status, pageNum, pageSize);
        return ResponseResult.success(page);
    }

    /**
     * 获取收入汇总信息
     * @return 收入汇总信息
     */
    @GetMapping("/summary")
    @PreAuthorize("hasAuthority('income:view')")
    public ResponseResult<Map<String, Object>> getIncomeSummary() {
        log.info("获取收入汇总信息");
        Map<String, Object> summary = incomeService.getIncomeSummary();
        return ResponseResult.success(summary);
    }

    /**
     * 获取最近收入记录
     * @param limit 限制数量，默认5
     * @return 最近收入记录列表
     */
    @GetMapping("/recent")
    @PreAuthorize("hasAuthority('income:view')")
    public ResponseResult<List<Map<String, Object>>> getRecentIncomeRecords(
            @RequestParam(defaultValue = "5") Integer limit) {
        log.info("获取最近收入记录，数量：{}", limit);
        List<Map<String, Object>> records = incomeService.getRecentIncomeRecords(limit);
        return ResponseResult.success(records);
    }
}